﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Battle
{
    public class PVPBattleOver : IBattleOver
    {

        public BattleType GetBattleType()
        {
            return BattleType.PVP;
        }

        bool localResult;
        bool remoteResult;

        OnBattleResult onResult;

        public PVPBattleOver (OnBattleResult onResult)
        {
            this.onResult = onResult;
        }

        public void Excute()
        {
            localResult = BattleManager.Instance.battleViewCtrl.winCamp == BattleCamp.Attacker;
            remoteResult = false;

            if (IsGuidePVPBattle())
            {
                GuidePVPFightResult();
            }
            else
            {
                PVPFightResult();
            }
            End();
        }

        bool IsGuidePVPBattle()
        {
            return GuideManager.Instance != null && GuideManager.Instance.GetRunningGuide().Equals("guide_pvp");
        }

        void GuidePVPFightResult()
        {
            var msg = StaticData.lastBattleEndMsg as ProtoBuf.Message.SCPvpFightResultGuide;
            var winPlayerUID = localResult ? StaticData.curBattleData.attackFighters[0].role.playerUID :
                StaticData.curBattleData.defendFighters[0].role.playerUID;
            if (winPlayerUID != msg.winPlayerId)
            {
                remoteResult = !localResult;
            }
            else
            {
                remoteResult = localResult;
            }

            StaticData.isLastBattleWin = remoteResult;
        }

        void PVPFightResult()
        {
            var msg = StaticData.lastBattleEndMsg as ProtoBuf.Message.SCPvpFightResult;
            var winPlayerUID = localResult ? StaticData.curBattleData.attackFighters[0].role.playerUID :
                StaticData.curBattleData.defendFighters[0].role.playerUID;
            if (winPlayerUID != msg.winPlayerId)
            {
                remoteResult = !localResult;
            }
            else
            {
                remoteResult = localResult;
            }
            StaticData.isLastBattleWin = remoteResult;
        }

        public void End()
        {

            if (onResult!= null)
            {
                onResult(remoteResult);
            }
        }

    }
}
